#!/bin/python3
# -*- coding: UTF-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.dates as mdates
from matplotlib.ticker import ScalarFormatter
from matplotlib.ticker import LogFormatter
from matplotlib.ticker import MultipleLocator
from matplotlib.ticker import FormatStrFormatter
from matplotlib.font_manager import findfont, FontProperties  
import matplotlib.font_manager as font_manager

from matplotlib.ticker import ScalarFormatter
from matplotlib.pyplot import MultipleLocator
from matplotlib import ticker
import common
import seaborn as sns
sns.set_theme()
# sns.set_style("white")
sns.set_theme(style="ticks", font_scale=1.33)
sns.set_context("paper", font_scale=1.5)
plt.rcParams['xtick.direction'] = 'in'#将x的刻度线方向设置向内
plt.rcParams['ytick.direction'] = 'in'#将y的刻度方向设置向内
plt.rcParams['font.family']=['Sarasa Mono SC'] #用来正常显示中文标签
txt_percentile = "执行时间百分位"
txt_executiontime = "执行时间(μs)"


color_base_max = 0.9


# colortbls = [	(color_base_max * 15.0/15.0,  color_base_max* 15.0/15.0, color_base_max*15.0/15.0), 
# 	(color_base_max * 14.0/15.0,  color_base_max* 14.0/15.0, color_base_max*14.0/15.0),
# 	(color_base_max * 13.0/15.0,  color_base_max* 13.0/15.0, color_base_max*13.0/15.0),
# 	(color_base_max * 12.0/15.0,  color_base_max* 12.0/15.0, color_base_max*12.0/15.0),
# 	(color_base_max * 11.0/15.0,  color_base_max* 11.0/15.0, color_base_max*11.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 9.0/15.0,  color_base_max* 9.0/15.0, color_base_max*9.0/15.0),
# 	(color_base_max * 8.0/15.0,  color_base_max* 8.0/15.0, color_base_max*8.0/15.0),
# 	(color_base_max * 7.0/15.0,  color_base_max* 7.0/15.0, color_base_max*7.0/15.0),
# 	(color_base_max * 6.0/15.0,  color_base_max* 6.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 5.0/15.0,  color_base_max* 5.0/15.0, color_base_max*5.0/15.0),
# 	(color_base_max * 4.0/15.0,  color_base_max* 4.0/15.0, color_base_max*4.0/15.0),
# 	(color_base_max * 3.0/15.0,  color_base_max* 3.0/15.0, color_base_max*3.0/15.0),
# 	(color_base_max * 2.0/15.0,  color_base_max* 2.0/15.0, color_base_max*2.0/15.0),
# 	(color_base_max * 1.0/15.0,  color_base_max* 1.0/15.0, color_base_max*1.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0)
# 	]

colortbls = [	(color_base_max * 130/255,  color_base_max* 176/255, color_base_max*210/255), # mutex
	(color_base_max * 12.0/14.0,  color_base_max* 12.0/14.0, color_base_max*12.0/14.0),  # pspin
	(color_base_max * 12.0/14.0,  color_base_max* 12.0/14.0, color_base_max*12.0/14.0),  # spin
	(color_base_max * 130/255,  color_base_max* 176/255, color_base_max*210/255), # ticket
	(color_base_max * 130/255,  color_base_max* 176/255, color_base_max*210/255), # clh
	(color_base_max * 130/255,  color_base_max* 176/255, color_base_max*210/255), # mcs
	(color_base_max * 147/255,  color_base_max* 207/255, color_base_max*201/255), # flat
	(color_base_max * 147/255,  color_base_max* 207/255, color_base_max*201/255), # ccsync
	(color_base_max * 147/255,  color_base_max* 207/255, color_base_max*201/255), # dsmsync
	(color_base_max * 147/255,  color_base_max* 207/255, color_base_max*201/255), # hsync
	(color_base_max * 190/255,  color_base_max* 184/255, color_base_max*220/255), # rcl
	(color_base_max * 190/255,  color_base_max* 184/255, color_base_max*220/255), # ffwd
	(color_base_max * 147/255,  color_base_max* 207/255, color_base_max*201/255), # sffwd
	(color_base_max * 250/255,  color_base_max* 127/255, color_base_max*111/255),
	(color_base_max * 250/255,  color_base_max* 127/255, color_base_max*111/255),
	(color_base_max * 250/255,  color_base_max* 127/255, color_base_max*111/255),
	(color_base_max * 250/255,  color_base_max* 127/255, color_base_max*111/255),
	(color_base_max * 250/255,  color_base_max* 127/255, color_base_max*111/255),
	]

# colortbls = [	(color_base_max * 10.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 6.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 6.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 10.0/15.0, color_base_max*10.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 6.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 6.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 6.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 10.0/15.0,  color_base_max* 6.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 14.0/15.0,  color_base_max* 14.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 14.0/15.0,  color_base_max* 14.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 14.0/15.0,  color_base_max* 14.0/15.0, color_base_max*6.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0),
# 	(color_base_max * 0.0/15.0,  color_base_max* 0.0/15.0, color_base_max*0.0/15.0)
# 	]

lib_list = ['mutex', 	'pspin', 'spin', 'ticket', 	'clh', 	'mcs', 'flat', 'ccsync', 'dsmsync', 	'hsync', 'rcl', 'ffwd', 'sffwd', 'cacflat', 'cacccsyn', 'cacdsmsyn', 'cachsyn', 'cacsffwd']
markers = ['x', 	'h', 	'v', 	'<', 		'>', 	's', 	'+', 	'd', 	'o', 		'D', 	'^', 	'x',	'h', 	'+', 'd', 'o', 'D', 'h']
# bencn_baseline = 'wfslab'


def read_data(file_path):
	data = np.loadtxt(file_path,dtype=str,delimiter=' ')
	# print (data)
	return data

def get_test_data(data, allocator_name):
	ret = data[data[:, 0] == allocator_name]
	# 裁减

	x_series = data[0, 1:]
	# print (x_series.astype(float))
	return x_series, ret[:, 1:].astype(float)


def get_test_data_x_y(data, allocator_name):
	x_name, y = get_test_data(data, allocator_name)
	x_num = np.unique(x_name).size
	# print (x_name)
	# x_name_row = int(x_name.size / x_num)
	# x_name = x_name.reshape(x_name_row, x_num)

	# print (x_name)
	# y = y[:, column].astype(typ)
	# y = np.array(y)

	# y = y.reshape(x_name_row, x_num)
	# y.
	return x_name, np.mean(y, axis=0), np.std(y, axis=0)

def calculate_data(data, allocator_name_lists):
	ry = []
	ry_std = []

	for allocator in allocator_name_lists:
		tx, ty, tstd = get_test_data_x_y(data, allocator)
		ry.append(ty)
		ry_std.append(tstd)

	rx = tx
	ry = np.array(ry)
	ry_std = np.array(ry_std)
	return rx, ry, ry_std


def plot_subfig_avg(data_x, data_avg_y, data_std, axs, ylimit):
	xticks = data_x.astype(float)
	i = 0
	# print (xticks)
	# print ( data_avg_y[i])

	for label in lib_list:
		print("current", label)
		print(data_avg_y[i])
		# axs.errorbar(xticks, data_avg_y[i], yerr = data_std[i],  ecolor=colortbls[i%len(lib_list)], fmt=".k", marker = "", alpha = 0.667, elinewidth=1, capsize=2)
		axs.plot(xticks, data_avg_y[i], color=colortbls[i], label=label, marker=markers[i], markerfacecolor='none', markersize = 4)
		i+=1

def set_axs_label(axs, data_x, data_avg_y, rects, xlabel, ylabel, ylimit, use_log):
	xticks = data_x.astype(float) 
	# print (xticks)
	axs.set_xscale("logit")
	if use_log != 0:
		axs.set_yscale("log")
	axs.yaxis.set_major_formatter(ticker.ScalarFormatter())
	axs.set_xticks(xticks)
	axs.set_xticklabels((np.round(xticks*100.0, 6)).astype(str))
	# print ((np.array(xticks_name).astype(np.float64)*100).astype(str))
	axs.set_xlabel(xlabel)
	axs.set_ylabel(ylabel)

	axs.get_yaxis().get_major_formatter().set_scientific(False)
	axs.set_ylim(ylimit)

def plot_x86_arm(data_x, data_avg_y, data_std, data_avg_y_arm, data_std_arm, xlabel, ylabel, ylimit, scale):
	fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(9, 3))
	# plt.xticks(rotation=120)
	data_avg_y = np.true_divide(data_avg_y ,scale)
	data_std = np.true_divide(data_std ,scale)
	data_avg_y_arm = np.true_divide(data_avg_y_arm ,scale)
	data_std_arm = np.true_divide(data_std_arm ,scale)

	rects = plot_subfig_avg(data_x, data_avg_y, data_std, axs[0], ylimit)
	set_axs_label(axs[0], data_x, data_avg_y, rects, xlabel, ylabel, ylimit, 1)
	axs[0].minorticks_off()

	rects = plot_subfig_avg(data_x, data_avg_y_arm, data_std_arm, axs[1], ylimit)
	set_axs_label(axs[1], data_x, data_avg_y_arm, rects, xlabel, "", ylimit, 0)
	axs[1].minorticks_off()

	# plt.legend(loc='upper center', bbox_to_anchor=(-0.11, 1.27), frameon=False, ncol= len(lib_list)/2 )
	# plt.tick_params(axis='y', which='minor')
	plt.legend(loc='upper center', bbox_to_anchor=(-0.11, 1.28), frameon=False, labelspacing=0.1, handletextpad=0.1, columnspacing=0.2, ncol= 9 )
	matplotlib.pyplot.subplots_adjust(left=0.08, right = 0.98, bottom = 0.15, top = 0.85, wspace = 0.11 )

	return fig, axs

# x86
data = read_data('./wcet_13700k.tsv')

# data1 = read_data('../api/test/wcettest_freeout.csv')

# print (data, data1[1:])
# print (data[:, :-1])
data = data[:, :-1]

# print (get_test_data(data, 'wfspan'))
# x, tdata = get_bench_data(data, 'sys')
alloc_x, alloc_y, alloc_std = calculate_data(data, lib_list)
print('13700k percentiles')
print (alloc_y)
# print (alloc_x)
data = read_data('./wcet_sdm888.tsv')
data = data[:, :-1]
# print (alloc_y)
# x, tdata = get_bench_data(data, 'sys')
aarch64_alloc_x, aarch64_alloc_y, aarch64_alloc_std = calculate_data(data, lib_list)
print('aarch64 percentiles')
print (aarch64_alloc_y)

plot_x86_arm(alloc_x, alloc_y, alloc_std, aarch64_alloc_y, aarch64_alloc_std, txt_percentile, txt_executiontime, (-1, 1000), 1000 )


plt.savefig("./fig/"+"wcet.pdf", format = "pdf")
# plot_x86_arm(free_x, free_y, free_std, aarch64_free_y, aarch64_free_std, 'free请求延迟百分位', '请求延迟 (μs)', (-1, 1100), 1000)

# matplotlib.pyplot.subplots_adjust(left=0.06, right = 0.99, bottom = 0.15, top = 0.93, wspace = 0.10 )


# data = read_data('../api/test/wcettest_memreqout.csv')

# req_x, req_y, req_std = calculate_data(data, lib_list)

# data = read_data('../api/test/wcettest_memreqout_arm.csv')

# aarch64_req_x, aarch64_req_y, aarch64_req_std = calculate_data(data, lib_list)


# plot_x86_arm(req_x, req_y, req_std, aarch64_req_y, aarch64_req_std, '百分位数(%)', '请求执行时间(纳秒)', (-1024, 1000*1000) )

# plot_x86_arm(alloc_x, alloc_y, alloc_std, aarch64_alloc_y, aarch64_alloc_std, '百分位数(%)', '请求执行时间(纳秒)', (-1024, 1024 * 1024) )

# plot_x86_arm(free_x, free_y, free_std, aarch64_free_y, aarch64_free_std, '百分位数(%)', '请求执行时间(纳秒)', (-1024, 1024 * 1024) )

plt.show()